﻿using System;
using System.ComponentModel;
using System.IO;
using System.IO.IsolatedStorage;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Boxi.Extensions;

namespace Boxi
{
    public class PersistentSettings : INotifyPropertyChanged
    {
        public static PersistentSettings Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new PersistentSettings();
                return _instance;
            }
            set { _instance = value; }
        }
        private static PersistentSettings _instance;

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
            if (!propertyName.Contains("Brush"))
                Save();
        }


        private const String SettingsKey = "Universal";
        /// <summary>
        /// 
        /// </summary>
        public Boolean IsSoundEnabled
        {
            get
            {
                return _isSoundEnabled;
            }
            set
            {
                _isSoundEnabled = value;
                NotifyPropertyChanged("IsSoundEnabled");
            }
        }
        private Boolean _isSoundEnabled = true;

        /// <summary>
        /// 
        /// </summary>
        public Boolean IsVibrationEnabled
        {
            get
            {
                return _isVibrationEnabled;
            }
            set
            {
                _isVibrationEnabled  = value;
                NotifyPropertyChanged("IsVibrationEnabled");
            }
        }
        private Boolean _isVibrationEnabled = true;

        /// <summary>
        /// 
        /// </summary>
        public Color AccentColor
        {
            get
            {
                return _accentColor;
            }
            set
            {
                _accentColor = value;
                NotifyPropertyChanged("AccentColor");
                NotifyPropertyChanged("AccentBrush");
            }
        }
        private Color _accentColor;
        public SolidColorBrush AccentBrush
        {
            get { return new SolidColorBrush(AccentColor); }
        }

        /// <summary>
        /// 
        /// </summary>
        public Color SecondaryAccentColor
        {
            get
            {
                return _secondaryAccentColor;
            }
            set
            {
                _secondaryAccentColor = value;
                NotifyPropertyChanged("SecondaryAccentColor");
                NotifyPropertyChanged("SecondaryAccentBrush");
            }
        }
        private Color _secondaryAccentColor;
        public SolidColorBrush SecondaryAccentBrush
        {
            get { return new SolidColorBrush(SecondaryAccentColor); }
        }

        /// <summary>
        /// 
        /// </summary>
        private void Save()
        {
            if (!IsConstructed)
                return;

            String rawData = String.Format("{0}-{1}-{2}-{3},{4}-{5}-{6}-{7},{8},{9}", 
                this.AccentColor.A,
                this.AccentColor.R,
                this.AccentColor.G,
                this.AccentColor.B,
                this.SecondaryAccentColor.A,
                this.SecondaryAccentColor.R,
                this.SecondaryAccentColor.G,
                this.SecondaryAccentColor.B,
                this.IsSoundEnabled, 
                this.IsVibrationEnabled);

            if (IsolatedStorageSettings.ApplicationSettings.Contains(SettingsKey))
                IsolatedStorageSettings.ApplicationSettings[SettingsKey] = rawData;
            else
                IsolatedStorageSettings.ApplicationSettings.Add(SettingsKey, rawData);
            IsolatedStorageSettings.ApplicationSettings.Save();
        }
        public void Reset()
        {
            if (IsolatedStorageSettings.ApplicationSettings.Contains(SettingsKey))
                IsolatedStorageSettings.ApplicationSettings.Remove(SettingsKey);
        }

        public PersistentSettings()
        {
            if (IsolatedStorageSettings.ApplicationSettings != null && IsolatedStorageSettings.ApplicationSettings.Contains(SettingsKey))
            {
                try
                {
                    var raw = IsolatedStorageSettings.ApplicationSettings[SettingsKey].ToString().Split(',');
                    var rawAccent = raw[0].Split('-');
                    var raSecondaryAccent = raw[1].Split('-');
                    AccentColor = Color.FromArgb(Byte.Parse(rawAccent[0]), Byte.Parse(rawAccent[1]), Byte.Parse(rawAccent[2]), Byte.Parse(rawAccent[3]));
                    SecondaryAccentColor = Color.FromArgb(Byte.Parse(raSecondaryAccent[0]), Byte.Parse(raSecondaryAccent[1]), Byte.Parse(raSecondaryAccent[2]), Byte.Parse(raSecondaryAccent[3]));
                    IsSoundEnabled = Boolean.Parse(raw[2]);
                    IsVibrationEnabled = Boolean.Parse(raw[3]);
                }
                catch (Exception ex)
                {
#if (DEBUG)
                    throw new Exception("PersistentSettings load Error", ex);
#endif
                }
            }
            if (SecondaryAccentColor.A == 0 && SecondaryAccentColor.R == 0 && SecondaryAccentColor.G == 0 && SecondaryAccentColor.B == 0)
            {
                AccentColor = (Color)Application.Current.Resources["PhoneAccentColor"];
                SecondaryAccentColor = AccentColor.GetComplimentaryColor();
            }
            IsConstructed = true;
        }
        public bool IsConstructed { get; set; }
    }
}
